package qdu.rg05.test_music_srv;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Act1 extends Activity {
    Intent it, it_bind;
    Myconn mc;
    Srv_2.Mybinder mb;
    MediaPlayer mp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ly_act1);

//        mp = MediaPlayer.create(this,R.raw.main);//创建媒体对象（媒体对应的当前上下文，媒体数据资源访问）
//        mp.setLooping(true);

        View.OnClickListener mylis = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch(view.getId()){
                    case R.id.btn_nobind:
                        it = new Intent(Act1.this, Srv_1.class);
                        startService(it);
                        break;
                    case R.id.btn_stop_nobind:
                        if(it != null)
                            stopService(it);
                        break;
                }
            }
        };
        findViewById(R.id.btn_stop_nobind).setOnClickListener(mylis);
        findViewById(R.id.btn_nobind).setOnClickListener(mylis);
        //*********************************************************8
        View.OnClickListener mylis_bind = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch(view.getId()){
                    case R.id.btn_bind_create:
                        it_bind = new Intent(Act1.this, Srv_2.class);
                        mc = new Myconn();
                        bindService(it_bind,mc, Context.BIND_AUTO_CREATE);
                        //Log.i("mytag", String.valueOf(mc.hashCode()));
                        break;
                    case R.id.btn_bind_stop:
                        try{
                            if(mb != null)
                                mb = null;
                            unbindService(mc);
                        }catch(Exception e){

                        }
                        break;
                    case R.id.btn_bind_getsrv:
                        if(mb != null) {
                            mb.getService();
                            mb.start_or_pause();
                        }
                        break;
                    case R.id.btn_m_loop:
                        if(mb != null) {
                            mb.set_looping();
                            ((TextView) findViewById(R.id.tv_loop_state))
                                    .setText(mb.get_looping()?"循环":"单放");
                        }
                        break;
                    case R.id.btn_m_stop:
                        if(mb!=null)
                            mb.music_stop();
                }
            }
        };
        findViewById(R.id.btn_bind_create).setOnClickListener(mylis_bind);
        findViewById(R.id.btn_bind_stop).setOnClickListener(mylis_bind);
        findViewById(R.id.btn_bind_getsrv).setOnClickListener(mylis_bind);
        findViewById(R.id.btn_m_loop).setOnClickListener(mylis_bind);
        findViewById(R.id.btn_m_stop).setOnClickListener(mylis_bind);
        Log.i("mytag", "Act1 oncreat...");
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if(mp != null && !mp.isPlaying())
//            mp.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        if(mp != null)
//            mp.stop();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        if(mp!=null && mp.isPlaying())
//            mp.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("mytag", "Act1 ondestroy...");
//        if(mp != null)
//            mp.release();
    }

    class Myconn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.i("mytag","service connected....");
            mb = (Srv_2.Mybinder) iBinder;
            //((Srv_2.Mybinder)iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.i("mytag","service disconnected....");
        }
    }
}
